package com.zy.socket.udp;

import java.io.IOException;
import java.net.*;

/**
 * @Author Gong
 * @Description
 * @dATE 2021/12/19 16:36
 **/
public class Send {
    public static void main(String[] args) {
        //创建DatagramSocket对象，绑定端口3456
        DatagramSocket sendSocket = null;
        try {
            sendSocket = new DatagramSocket(3456);
            //准备好要发送的数据，类型为byte[]
            String string = "Hello,I come form ICSS!";
            byte[] databyte = new byte[100];
            databyte = string.getBytes();
//创建数据报，封装了要发送的数据，数据长度，服务器地址，以及服务器端口为5000
            DatagramPacket sendPacket = new DatagramPacket(databyte,
                    string.length(), InetAddress.getByName("127.0.0.1"), 5000);
//使用DatagramSocket对象将数据报sendPacket发送到服务器
            sendSocket.send(sendPacket);
            System.out.println("发送数据:" + string);

        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
